package com.beilang.base.core.dao;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapClient;


public abstract class BaseDAO extends HibernateDaoSupport {

	private SqlMapEngine sqlMapEngine = new SqlMapEngine();

	protected final Log log = LogFactory.getLog(getClass());

	public final SqlMapClientTemplate getSqlMapClientTemplate() {
		return sqlMapEngine.getSqlMapClientTemplate();
	}

	public final void setSqlMapClient(SqlMapClient sqlMapClient) {
		sqlMapEngine.setSqlMapClient(sqlMapClient);
	}

	public Object get(Class clazz, Serializable id) {
		return getHibernateTemplate().get(clazz, id);
	}

	public List getAll(Class clazz) {
		return getHibernateTemplate().loadAll(clazz);
	}

	public void remove(Class clazz, Serializable id) {
		getHibernateTemplate().delete(get(clazz, id));
	}

	public Object save(Object o) {
		return getHibernateTemplate().merge(o);
	}

	public List queryForList(String statementName, Object parameterObject,
			int skipResults, int maxResults) throws DataAccessException {
		return getSqlMapClientTemplate().queryForList(statementName,
				parameterObject, skipResults, maxResults);
	}

	public List queryForList(String statementName, int skipResults,
			int maxResults) throws DataAccessException {
		return getSqlMapClientTemplate().queryForList(statementName,
				skipResults, maxResults);
	}

	public List queryForList(final String statementName,
			final Object parameterObject) throws DataAccessException {
		return getSqlMapClientTemplate().queryForList(statementName,
				parameterObject);
	}

	public List queryForList(String statementName) throws DataAccessException {
		return getSqlMapClientTemplate().queryForList(statementName);
	}

	public Object queryForObject(final String statementName,
			final Object parameterObject, final Object resultObject)
			throws DataAccessException {
		return getSqlMapClientTemplate().queryForObject(statementName,
				parameterObject, resultObject);
	}

	public Object queryForObject(final String statementName,
			final Object parameterObject) throws DataAccessException {
		return getSqlMapClientTemplate().queryForObject(statementName,
				parameterObject);
	}

	public Object queryForObject(String statementName)
			throws DataAccessException {
		return getSqlMapClientTemplate().queryForObject(statementName);
	}
	

    public Map objectToMap (Object to) {
        try {
            return PropertyUtils.describe(to);           
        } catch (IllegalAccessException e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getStackTrace());
            }
        } catch (InvocationTargetException e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getStackTrace());
            }
        } catch (NoSuchMethodException e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getStackTrace());
            }
        }
        return new HashMap();
    }


	
}